package com.roc.service.impl.settingImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.dao.IorgDao;
import com.roc.dao.setting.ISettingFlagDao;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.CalcuBean;
import com.roc.pojo.DuizhangBean;
import com.roc.pojo.Esmidinfo;
import com.roc.pojo.ExcelBean;
import com.roc.pojo.Operators;
import com.roc.pojo.SettFlagBean;
import com.roc.pojo.SettingBean;
import com.roc.pojo.SettingItemBean;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettflagService;
import com.roc.util.constant.DateFormat;
import com.roc.util.constant.FrozenStatus;
import com.roc.util.constant.OperatorsStatus;
import com.roc.util.constant.SysLogType;
import com.roc.web.action.setting.CalcuSearchCondition;
import com.roc.web.action.setting.ReviewSettingSearchCondition;

@Service
@Transactional
public class SettflagServiceImpl implements ISettflagService {
	private Logger logger = Logger.getLogger(SettflagServiceImpl.class);
	@Autowired
	private IorgDao orgDao;
	@Autowired
	ISQLBaseDao sqlBaseDao;
	@Autowired
	private IBaseDao baseDao;
	@Autowired
	private ISettingFlagDao settingFlagDao;

	@Override
	public List<Object> querycalcuSummoney(CalcuSearchCondition searchCondition) throws ServiceException {

		List<Object> objs=null;
		try {
			objs= settingFlagDao.querycalcuSummoney(searchCondition);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算信息失败~！");
		}

		return objs;
	}
	@Override
	public List<SettFlagBean> querySettingFlagList() throws ServiceException {
		List<SettFlagBean> settingflagList = new ArrayList<SettFlagBean>();

		try {
			// settingflagList=settingFlagDao.querySettingFlagList();
			String sql = "select * from  T_SETTINGFLAG_INFO";
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql);
			if (objList != null && objList.size() > 0) {
				Iterator iterator = objList.iterator();
				while (iterator.hasNext()) {
					SettFlagBean merchant = new SettFlagBean();
					Object[] obj = (Object[]) iterator.next();
					merchant.setSettingFlageId((String) obj[0]);
					merchant.setSettingFlagName((String) obj[1]);
					settingflagList.add(merchant);
				}
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算标示失败~！");
		}

		return settingflagList;
	}

	@Override
	public boolean frozenApply(AuditingItem auditing, SettingBean sett,
			String stuts) throws ServiceException {
		boolean result = false;
		try {
			result = settingFlagDao.frozenApply(auditing, sett);
			/** *****添加审核结果成功---记录日志****** */
			if (stuts.equals(FrozenStatus.frozen.getCode())) {
				baseDao.addSyslog(SysLogType.SETTING_FROZEN.getCode(),
						SysLogType.SETTING_FROZEN.getName(), auditing
								.getOperators(), "操作员"
								+ auditing.getOperators() + "【"
								+ auditing.getAudit_name() + "】【"
								+ sett.getId() + "】成功");
			} else if (stuts.equals(FrozenStatus.unfrozen.getCode())) {
				baseDao.addSyslog(SysLogType.SETTING_UNFROZEN.getCode(),
						SysLogType.SETTING_UNFROZEN.getName(), auditing
								.getOperators(), "操作员"
								+ auditing.getOperators() + "【"
								+ auditing.getAudit_name() + "】【"
								+ sett.getId() + "】成功");
			} else if (stuts.equals("reviewfrozen")) {
				baseDao.addSyslog(SysLogType.SETTING_REVIEWFROZEN.getCode(),
						SysLogType.SETTING_REVIEWFROZEN.getName(), auditing
								.getOperators(), "操作员"
								+ auditing.getOperators() + "【"
								+ auditing.getAudit_name() + "】【"
								+ sett.getId() + "】成功");
			} else if (stuts.equals("reviewunfrozen")) {
				baseDao.addSyslog(SysLogType.SETTING_REVIEWUNFROZEN.getCode(),
						SysLogType.SETTING_REVIEWUNFROZEN.getName(), auditing
								.getOperators(), "操作员"
								+ auditing.getOperators() + "【"
								+ auditing.getAudit_name() + "】【"
								+ sett.getId() + "】成功");
			}

		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("审核失败~！");
		}
		return result;
	}
	@Override
	public boolean KYApply(AuditingItem auditing, Esmidinfo esm,
			String stuts) throws ServiceException {
		boolean result = false;
		try {
			baseDao.editSave(auditing);
			baseDao.editUpdate(esm);
			
			if(stuts.equals("A4")){
				
				String sql="update estidinfo es set es.flag=2 where es.mid='"+esm.getMid()+"'";
				sqlBaseDao.executeBySql(sql);
				
				String sql1 = "update Esmidinfo_Tmp t set t.flag=2,t.mer_status='"+stuts+"' where t.mid='"+esm.getMid()+"'";
				sqlBaseDao.executeBySql(sql1);
			}else{
				String sql1 = "update Esmidinfo_Tmp t set t.mer_status='"+stuts+"' where t.mid='"+esm.getMid()+"'";
				sqlBaseDao.executeBySql(sql1);
			}
			/** *****添加审核结果成功---记录日志****** */
			
				baseDao.addSyslog(SysLogType.KY_STATUS.getCode(),
						SysLogType.KY_STATUS.getName(), auditing
								.getOperators(), "操作员"
								+ auditing.getOperators() + "把【"
								+ esm.getMid() + "】风控等级变更为【"
								+ auditing.getAudit_name() + "】成功");
			

		} catch (DaoException e) {
			logger.debug(e.getMessage());
			result=true;
			throw new ServiceException("审核失败~！");
		}
		return result;
	}

	@Override
	public SettingBean querySettingBean(String id) throws ServiceException {

		SettingBean sett = new SettingBean();
		try {
			sett = settingFlagDao.querySettingBean(id);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算信息失败~！");
		}

		return sett;
	}
	@Override
	public SettingBean querySettingBean(String merchantno,String bathno) throws ServiceException {

		SettingBean sett = new SettingBean();
		try {
			sett = settingFlagDao.querySettingBean(merchantno,bathno);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算信息失败~！");
		}

		return sett;
	}
	@Override
	public int queryStatusCount(List<Long> iddlist) throws ServiceException {

		int i =0;
		try {
			i = settingFlagDao.queryStatusCount(iddlist);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算信息失败~！");
		}

		return i;
	}
	
	@Override
	public int querycaucleStatusCount(List<String> iddlist,String bathno) throws ServiceException {

		int i =0;
		try {
			i = settingFlagDao.querycaucleStatusCount(iddlist,bathno);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算信息失败~！");
		}

		return i;
	}
	@Override
	public double querySummoney(ReviewSettingSearchCondition searchCondition) throws ServiceException {

		double i =0;
		try {
			i = settingFlagDao.querySummoney(searchCondition);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算信息失败~！");
		}

		return i;
	}
	
	@Override
	public void excuSettingPro() throws ServiceException {
		try {
			settingFlagDao.excuSettingPro();
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("执行清算过程失败~！");
		}
	}
	@Override
	public void excuSettingLoadPro() throws ServiceException {
		int i=0;
		try {
			settingFlagDao.excuSettingLoadPro();
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("执行清算过程失败~！");
		}
	}
	@Override
	public int excuKQSettingPro(String minDate,String maxDate) throws ServiceException {
		int i=0;
		try {
			i=settingFlagDao.excuKQSettingPro(minDate,maxDate);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("执行清算过程失败~！");
		}
		return i;
	}
	@Override
	public int excuHRTSettingPro(String minDate,String maxDate) throws ServiceException {
		int i=0;
		try {
			System.out.print("service");
			i=settingFlagDao.excuHRTSettingPro(minDate,maxDate);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("执行清算过程失败~！");
		}
		return i;
	}
	@Override
	public void excuCalcuPro() throws ServiceException {

		try {
			System.out.print("service");
			settingFlagDao.excuCalcuPro();
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("执行清算过程失败~！");
		}
	}

	@Override
	public String maxserid(String settdate) throws ServiceException {
		String str = "1";
		try {

			str = settingFlagDao.maxserid( settdate);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("获取最大明细编号失败~！");
		}
		return str;
	}

	@Override
	public boolean reviewsetting(AuditingItem auditing, SettingBean sett,
			String stuts, List<SettingItemBean> list) throws ServiceException {
		boolean result = false;
		try {
			result = settingFlagDao.reviewsetting(auditing, sett, list);
			if (stuts.equals("W")) {
				/** *****添加审核结果成功---记录日志****** */
				baseDao.addSyslog(SysLogType.SETTING_REVIEW.getCode(),
						SysLogType.SETTING_REVIEW.getName(), auditing
								.getOperators(), "操作员"
								+ auditing.getOperators() + "【"
								+ auditing.getAudit_name() + "】【"
								+ sett.getId() + "】成功");

			}

		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("审核失败~！");
		}
		return result;
	}
	
	
	@Override
	public boolean insertCHZ(Map<String,DuizhangBean> map,Map<String,DuizhangBean> map1,Map<String,DuizhangBean> map2) throws ServiceException {
		boolean result = false;
		try {
			//result = settingFlagDao.insertCHZ(map, map1);
			
			
			/*******更新清算表*******/
			Iterator cauite = map2.keySet().iterator();
			while(cauite.hasNext()){	
				Object key = cauite.next();     
				DuizhangBean dz = new DuizhangBean();
				dz = (DuizhangBean) map2.get(key); 
				settingFlagDao.updatecalcu(dz);
			}
			
			/*******对账文件插入查错账表*******/
			Iterator it = map.keySet().iterator();
			while(it.hasNext()){	
				Object key = it.next();  
				DuizhangBean dz = new DuizhangBean();
				
				 dz = (DuizhangBean) map.get(key); 
				 
				 settingFlagDao.deleteDZ(dz);
					//查询是否是因为本地撤销或者冲正引起的查错账。
					String reversal=settingFlagDao.selectreversal(dz.getReqlogno(),dz.getReqdate(),dz.getReqtime());
					if(reversal!=null&&!"".equals(reversal)){
						if("5".equals(reversal.trim())){
							//通道有本地被撤销。
							dz.setDiffflag("3");
						}else if("1".equals(reversal.trim())){
							System.out.println(reversal);
							//通道有本地冲正失败。
							dz.setDiffflag("4");
						}else if("2".equals(reversal.trim())){
							//通道有本地被冲正。
							dz.setDiffflag("5");
						}
					}
					baseDao.editSave(dz);
					
			}
			/*******本地数据文件插入查错账表*******/
			Iterator ite = map1.keySet().iterator();
			while(ite.hasNext()){	
				Object key = ite.next();     
				DuizhangBean dz = new DuizhangBean();
				dz = (DuizhangBean) map1.get(key); 
				settingFlagDao.deleteDZ(dz);
				baseDao.editSave(dz);
			}
			result=true;
		} catch (Exception e) {
			logger.debug(e.getMessage());

			throw new ServiceException("审核失败~！");
		}
		return result;
	}
	
// 查询出款信息
	@Override
	public List<SettingItemBean> querySettingItemList() throws ServiceException {
		List<SettingItemBean> settingitemList = new ArrayList<SettingItemBean>();

		try {
			// settingflagList=settingFlagDao.querySettingFlagList();
			String sql = "select * from  t_settle_item t ";
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql);
			if (objList != null && objList.size() > 0) {
				Iterator iterator = objList.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					SettingItemBean sett = new SettingItemBean();
					if (obj[0] != null && !"".equals(obj[0])) {
						sett.setId(Long.valueOf(obj[0].toString()));
					}
					if (obj[1] != null && !"".equals(obj[1])) {
						sett.setSerial(obj[1].toString());
					}
					if (obj[2] != null && !"".equals(obj[2])) {
						sett.setAccountno(obj[2].toString());
					}
					if (obj[3] != null && !"".equals(obj[3])) {
						sett.setAccountname(obj[3].toString());
					}
					if (obj[4] != null && !"".equals(obj[4])) {
						sett.setAmt(obj[4].toString());
					}
					if (obj[5] != null && !"".equals(obj[5])) {
						sett.setBankname(obj[5].toString());
					}
					if (obj[6] != null && !"".equals(obj[6])) {
						sett.setBanklinkno(obj[6].toString());
					}
					// if (obj[7] != null && !"".equals(obj[7])) {
					// sett.setRemark(obj[7].toString());
					// }
					if (obj[8] != null && !"".equals(obj[8])) {
						sett.setMerchantno(obj[8].toString());
					}
					// if (obj[9] != null && !"".equals(obj[9])) {
					// sett.setSystime()(obj[9].toString());
					//					}
					if (obj[10] != null && !"".equals(obj[10])) {
						sett.setDelval(obj[10].toString());
					}

					settingitemList.add(sett);
				}
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询结算标示失败~！");
		}
		return settingitemList;
	}
	// 查询出款信息
	@Override
	public CalcuBean queryCalcuBean(String date,String time,String termtrace) throws ServiceException {
		List<Object> settingitemList = new ArrayList<Object>();
		CalcuBean calcuBean = new CalcuBean();
		try {
			calcuBean = settingFlagDao.queryCalcuBean(date, time, termtrace);
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("查询清算信息失败~！");
		}
		return calcuBean;
	}
	// 查询出款信息
	@Override
	public Map<String,DuizhangBean> queryDuizhangBean(String mindate,String maxdate,String finadate,String dateFlag) throws ServiceException {
		
		Map<String,DuizhangBean> map = new HashMap<String,DuizhangBean>();
		try {
			// settingflagList=settingFlagDao.querySettingFlagList();
			String sql="";
			if("1".equals(dateFlag)){
				 sql = "select t.etstrace,t.etsdate,t.etstime,t.account,t.amount,t.merno,t.termid,e.BELONG_BRANCH from ESTRNHISLOG t,ESMIDINFO e where e.mid=t.merno and t.etstrancode = '020000' and t.respcode = '00' and t.reversal='0' AND t.Etsdate||t.Etstime >= to_char(to_date('"+finadate+"','yyyymmdd')-1,'yyyymmdd')||'223000' AND t.Etsdate||t.Etstime <= '"+finadate+"'||'222959' ";
			}
			if("2".equals(dateFlag)){
				 sql = "select t.etstrace,t.etsdate,t.etstime,t.account,t.amount,t.merno,t.termid,e.BELONG_BRANCH from ESTRNHISLOG t,ESMIDINFO e where e.mid=t.merno and t.etstrancode = '020000' and t.respcode = '00' and t.reversal='0' AND t.Etsdate || t.Etstime >= to_char(to_date('"+mindate+"', 'yyyymmdd') - 1, 'yyyymmdd') ||'223000' AND t.Etsdate || t.Etstime <=to_char(to_date('"+maxdate+"', 'yyyymmdd'), 'yyyymmdd') ||'222959' ";
			}
			
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql);
			if (objList != null && objList.size() > 0) {
				Iterator iterator = objList.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					DuizhangBean sett = new DuizhangBean();
					String key="";
					if (obj[0] != null && !"".equals(obj[0])) {
						sett.setReqlogno(obj[0].toString().substring(6, 12));
						key=key+obj[0].toString().substring(6, 12);
					}
					if (obj[1] != null && !"".equals(obj[1])) {
						sett.setReqdate(obj[1].toString());
						key=key+obj[1].toString();
					}
					if (obj[2] != null && !"".equals(obj[2])) {
						sett.setReqtime(obj[2].toString());
						key=key+obj[2].toString();
					}
					if (obj[3] != null && !"".equals(obj[3])) {
						sett.setAccount(obj[3].toString());
					}
					if (obj[4] != null && !"".equals(obj[4])) {
						sett.setTraamt(obj[4].toString());
					}
					if (obj[5] != null && !"".equals(obj[5])) {
						sett.setHostmerchid(obj[5].toString());
						key=key+obj[5].toString();
					}
					if (obj[6] != null && !"".equals(obj[6])) {
						sett.setHosttermid(obj[6].toString());
						key=key+obj[6].toString();
					}
					if (obj[7] != null && !"".equals(obj[7])) {
						sett.setAccbranchid(obj[7].toString());
					}
					sett.setDiffflag("2");
					sett.setProcessflag("1");
					String time="";
					Date dt = new Date();  
				    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
				    time=sdf.format(dt); 
				    sett.setInsertt(time);
					map.put(key, sett);
				}
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage());
			throw new ServiceException("查询结算标示失败~！");
		}
		return map;
	}
	// 查询出款信息
	@Override
	public List<AuditingItem> queryAuditingItemList(String id) throws ServiceException {
		List<AuditingItem> auditinglist= new ArrayList<AuditingItem>();

		try {
			// settingflagList=settingFlagDao.querySettingFlagList();
			String sql = "SELECT a.audit_name, a.result_detail, a.operators,   to_char(a.dt_create, 'yyyymmdd'),T.datapath  from t_Auditing_item a,t_transjnls_settle T where T.id =a.obj_audited and a.obj_audited='"+id+"'";
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql);
			if (objList != null && objList.size() > 0) {
				Iterator iterator = objList.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					AuditingItem audit = new AuditingItem();
					if (obj[0] != null && !"".equals(obj[0])) {
						audit.setAudit_name(obj[0].toString());
					}
					if (obj[1] != null && !"".equals(obj[1])) {
						audit.setResult_detail(obj[1].toString());
					}
					
					if (obj[2] != null && !"".equals(obj[2])) {
						audit.setOperators(obj[2].toString());
					}
					if (obj[3] != null && !"".equals(obj[3])) {
						SimpleDateFormat formatDate = new SimpleDateFormat("yyyymmdd");
						
						Date date=formatDate.parse(obj[3].toString());
							
						audit.setDtCreate(date);
					}
					if (obj[4] != null && !"".equals(obj[4])) {
						audit.setPathUrl(obj[4].toString());
					}else{
						audit.setPathUrl("");
					}
					
					auditinglist.add(audit);
				}
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询审核记录失败~！");
		} catch (Exception e) {
			logger.debug(e.getMessage());
			System.out.println(e.getMessage());
			throw new ServiceException("查询审核记录失败~！");
		}
		return auditinglist;	
	}

	@Override
	public int frozencaucle(List<String> iddlist, SettingBean sett,String batchno)
			throws ServiceException {
		int i =0;
		try {
			
		//根据iddlist查询总金额，总手续费，总结算金额。
		//List<Object>  list = querySUM(batchno,iddlist);
			StringBuffer sql=new StringBuffer("    select sum(t.amount),sum(t.merfee),sum(t.calcuamount) from t_transjnls_calcu t where   t.settbatch='"+batchno+"' and t.etstrace in ('");
			for(int j=0;j<iddlist.size();j++){
				if(j!=iddlist.size()-1){
					sql.append(iddlist.get(j)+"','");
				}else{
					sql.append(iddlist.get(j));
				}
			}
			sql.append("')");
			
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql.toString());
		
		double amount =0;
		double merfee =0;
		double calcuamount=0;
		if (objList != null && objList.size() > 0) {
			Iterator iterator = objList.iterator();
			while (iterator.hasNext()) {
				Object[] obj = (Object[]) iterator.next();
				amount=amount+	Double.valueOf(String.valueOf(obj[0]));
				merfee=merfee+ Double.valueOf(String.valueOf(obj[1]));
				calcuamount=calcuamount+ Double.valueOf(String.valueOf(obj[2]));
			}
		}
		
		double t0 = (double) Math.round((sett.getTotalamt() -amount) * 100) / 100;
		double t1 = (double) Math.round((sett.getMerchantfee() -merfee) * 100) / 100;
		double t2 = (double) Math.round((sett.getMerchantsettleamt() -calcuamount) * 100) / 100;
	
		//判断拆分的金额是否为0；
		if(t0==0){
			sett.setFreezeflag("Q");
			baseDao.editUpdate(sett);	
		}else{
			sett.setTotalamt(t0);
			sett.setMerchantfee(t1);
			sett.setMerchantsettleamt(t2);
			sett.setTotalnum(sett.getTotalnum()-iddlist.size());
			baseDao.editUpdate(sett);	
			
			SettingBean newsett=copysett(sett);
			
			//生成一个新的批次号。年月日时分秒+六位随机数
			Random r = new Random(); 
			Double d = r.nextDouble(); 
			String s = d + ""; 
			s=s.substring(3,3+6); 
			String time="";
			Date dt = new Date();  
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
		    time=sdf.format(dt); 
		    String ordno = time+s;
		    //冻结的清算记录生成一条对应新的结算记录；
			newsett.setOrderno(ordno);
			newsett.setFreezeflag("Q");
			newsett.setTotalamt(amount);
			newsett.setMerchantfee(merfee);
			newsett.setMerchantsettleamt(calcuamount);
			newsett.setTotalnum(iddlist.size());
			Date date = new Date();
			newsett.setDt_create(date);
				baseDao.editSave(newsett);	
			
			//修改对应的清算记录的批次号。
			i=settingFlagDao.updatecalcu(iddlist, batchno, ordno);
			
			
			
		}

		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}
	public List<Object> querySUM(String bathno,List<String> iddlist) throws ServiceException {
		List<Object> settingflagList = new ArrayList<Object>();
		try {
			// settingflagList=settingFlagDao.querySettingFlagList();
			StringBuffer sql=new StringBuffer("    select sum(t.amount),sum(t.merfee),sum(t.calcuamount) from t_transjnls_calcu t where   t.settbatch='"+bathno+"' and t.etstrace in ('");
			for(int j=0;j<iddlist.size();j++){
				if(j!=iddlist.size()-1){
					sql.append(iddlist.get(j)+"','");
				}else{
					sql.append(iddlist.get(j));
				}
			}
			sql.append("')");
			
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql.toString());
			
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询失败~！");
		}

		return settingflagList;
	}
	public SettingBean copysett(SettingBean sett){
		SettingBean newsett = new SettingBean();
		if(sett!=null){
			newsett.setAccountname(sett.getAccountname());
			newsett.setAccountno(sett.getAccountno());
			newsett.setBankdeposit(sett.getBankdeposit());
			newsett.setBatchno(sett.getBatchno());
			newsett.setChanalfee(sett.getChanalfee());
			newsett.setDatapath(sett.getDatapath());
			newsett.setDeposino(sett.getDeposino());
			newsett.setFreezeflag(sett.getFreezeflag());
			newsett.setMerchantfee(sett.getMerchantfee());
			newsett.setMerchantname(sett.getMerchantname());
			newsett.setMerchantno(sett.getMerchantno());
			newsett.setMerchantsettleamt(sett.getMerchantsettleamt());
			newsett.setOrderno(sett.getOrderno());
			newsett.setOrgno(sett.getOrgno());
			newsett.setRemark(sett.getRemark());
			newsett.setSetdate(sett.getSetdate());
			newsett.setSettledate(sett.getSettledate());
			newsett.setSettleflag(sett.getSettleflag());
			newsett.setTotalamt(sett.getTotalamt());
			newsett.setTotalnum(sett.getTotalnum());
			newsett.setTransenddate(sett.getTransenddate());
			newsett.setTransstartdate(sett.getTransstartdate());
			newsett.setUserid(sett.getUserid());
		}
		return newsett;
	}
	@Override
	public void deleteDZ(DuizhangBean db) throws ServiceException {

		int i =0;
		try {
			 settingFlagDao.deleteDZ(db);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("删除差错帐失败~！");
		}
	}
	@Override
	public int queryDZcount(DuizhangBean db) throws ServiceException {

		int i =0;
		try {
			i = settingFlagDao.queryDZcount(db);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询查错账条数失败~！");
		}

		return i;
	}
	@Override
	public int queryTrancount(DuizhangBean db) throws ServiceException {

		int i =0;
		try {
			i = settingFlagDao.queryTrancount(db);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询本地数据信息失败~！");
		}

		return i;
	}
	@Override
	public String selectExcelName(String modelflag,String excelflag) throws ServiceException {
		String str = "";
		try {

			str = settingFlagDao.selectExcelName(modelflag,excelflag);
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("获取最大明细编号失败~！");
		}
		return str;
	}
	@Override
	public int updatesetting(String minDate, String maxDate, String modelflag,
			String excelflag, ExcelBean exb) throws ServiceException {
		int i=0;
		try {
		StringBuffer sb=new StringBuffer();
		if(!"".equals(modelflag)&&!"".equals(excelflag)){
			sb.append("update t_transjnls_settle t set t.loadflag='1' where 1=1 ");
			if("1".equals(excelflag)){
				sb.append(" and t.HOSTBRANTHNO='00000008' ");	
			}else if("2".equals(excelflag)){
				sb.append(" and t.HOSTBRANTHNO='00500380' ");	
			}
			
			if("g".equals(modelflag)){
				sb.append(" and t.acc_type='01'  and  t.merchantno not in(select ts.merno from t_settle_merno ts) ");	
			}else if("c".equals(modelflag)){
				sb.append(" and t.acc_type <> '01' and t.bankdeposit not like '中国银行%' and  t.merchantsettleamt<50000 and t.accountname not like '%·%'  and  t.merchantno not in(select ts.merno from t_settle_merno ts) ");		
			}else if("k".equals(modelflag)){
				sb.append(" and t.acc_type <> '01' and (t.bankdeposit  like '中国银行%' or  t.merchantsettleamt>50000) and t.accountname not like '%·%'  and  t.merchantno not in(select ts.merno from t_settle_merno ts) ");	
			}else if("d".equals(modelflag)){
				sb.append(" and t.acc_type <> '01' and t.accountname like '%·%'  and  t.merchantno not in(select ts.merno from t_settle_merno ts)");	
			}else if("w".equals(modelflag)){
				sb.append("   and  t.merchantno in(select ts.merno from t_settle_merno ts)");	
			}else if("q".equals(modelflag)){
				sb.append("   and  t.merchantno not in(select ts.merno from t_settle_merno ts)");	
			}
			sb.append(" and t.SETTLEDATE>='").append(minDate);
			sb.append("' and t.SETTLEDATE<='").append(maxDate).append("'");
		}
			i = sqlBaseDao.executeBySql(sb.toString());
			if(i!=0){
				baseDao.editSave(exb);
			}
			
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return i;
	}
	
	@Override
	public String getsettid() throws ServiceException {
		String str = "1";
		try {

			str = settingFlagDao.getsettid();
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("获取最大明细编号失败~！");
		}
		return str;
	}
}
